//
//  MemberCampaignView.m
//  HomePage
//
//  Created by xiong qi on 13-4-25.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "MemberCampaignView.h"
#import "MemberCampaignItemView.h"


@implementation MemberCampaignView
@synthesize dataSource;

- (id)initWithFrame:(CGRect)frame Data:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        
        wfView = [[WaterFallView alloc]initWithFrame:CGRectMake((frame.size.width-960)/2,0,960, frame.size.height)];
        [wfView setWfDelegate:self];
        [self addSubview:wfView];
        [wfView release];
        
        maskView = [[MaskBackView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
        maskView.delegate = self;
        maskView.alpha = 0;
        maskView.backgroundColor = [UIColor blackColor];
        
        largeImageView = [[ZoomImageView alloc] initWithFrame:maskView.bounds];
        [maskView addSubview:largeImageView];
        [largeImageView release];
        largeImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        UITapGestureRecognizer *tapGes=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapLargeImageView:)];
        [largeImageView addGestureRecognizer:tapGes];
        [tapGes release];
        
        self.dataSource = [self createDataSource:array];
        [wfView reloadData];
    }
    return self;
}


-(NSArray *)createDataSource:(NSArray *)array
{
    NSMutableArray * dataarray = [NSMutableArray array];
    NSString * imagepath = nil;
    for (int i=0; i<[array count]; i++) {
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        imagepath = [NSString stringWithFormat:@"local/activities/%@",[array objectAtIndex:i]];
        [dic setObject:imagepath forKey:Key_Data];
        [dic setObject:[NSNumber numberWithFloat:0] forKey:Key_Height];
        [dataarray addObject:dic];
    }
    
    return dataarray;
}

-(void)CellBeTouch:(MemberCampaignItemView*)cell
{
    
    largeImageView.imgView.image  = [MemberCampaignItemView GetImage:[cell.dataSource objectForKey:Key_Data]];;
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window.rootViewController.view addSubview:maskView];
    
    [UIView animateWithDuration:0.4
                     animations:^{
                         
                         maskView.alpha = 1.0;
                     }
                     completion:^(BOOL finished){
                         
                         
                     }];
}

-(void)RemoveView
{
    [UIView animateWithDuration:0.4
                     animations:^{
                         
                         maskView.alpha = 0.0;
                     }
                     completion:^(BOOL finished){
                         [maskView removeFromSuperview];
                         
                     }];
}
-(void) onTapLargeImageView:(UITapGestureRecognizer *)tapGesture{
    [self RemoveView];
}

#pragma WaterFallViewDelegate Methods

- (NSInteger)numberOfCellsInWaterFallView:(WaterFallView *)wfView
{
    return [dataSource count];
}

- (NSInteger)numberOfColumnsInWaterFallView:(WaterFallView *)wfView
{
    return 3;
}

- (UIView *)waterFallView:(WaterFallView *)wView cellAtIndex:(NSInteger)index
{
    NSString * identifier = @"MemberCampaignItemView";
    MemberCampaignItemView * cell = nil;
    cell = (MemberCampaignItemView * )[wfView dequeueReusableCellWithIdentifier:identifier];
    if(cell==nil)
    {
        cell = [[[MemberCampaignItemView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
        cell.reuseIdentifier = identifier;
        cell.parentDelegate = self;
    }
    
    NSObject * Info = [dataSource objectAtIndex:index];
    [cell performSelector:@selector(loadcelldata:) withObject:Info];
    
    return  cell;
}

- (void)waterFallView:(WaterFallView *)wfview willDisplayCell:(UIView<WaterFallCellDelegate> *)cell forIndex:(NSInteger)index
{
    [cell performSelector:@selector(ResetFrame) withObject:nil];
}

- (CGFloat)waterFallView:(WaterFallView *)wfView heightForCellAtIndex:(NSInteger)index
{
    NSMutableDictionary * Info = [dataSource objectAtIndex:index];
    if ([[Info objectForKey:Key_Height] floatValue] == 0) {
        [MemberCampaignItemView caculateCellHeight:Info];
    }
    
    return [[Info objectForKey:Key_Height] floatValue];
}

- (UIView *)headerForWaterFallView:(WaterFallView *)wfView
{
    return nil;
}

- (UIView *)footerForWaterFallView:(WaterFallView *)wfView
{
    return nil;
}

- (void)waterFallView:(WaterFallView *)wfview didBackupCell:(UIView<WaterFallCellDelegate> *)cell forIndex:(NSInteger)index
{
    MemberCampaignItemView * _cell = (MemberCampaignItemView*)cell;
    [_cell clearImage];
}


-(void)dealloc
{
    self.dataSource = nil;
    [maskView release];
    [super dealloc];
}


@end
